﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Security;
using Microsoft.Web.Services2.Security.Tokens;
using System.Data;
using System.Configuration;
using MySCM.Common.Log;

using System.Windows.Forms; // for MessageBox

namespace MySCM.Integration.Elementool.ElementoolIntegrator.Pull
{
    class Program
    {
        static void Main(string[] args)
        {
            StaticLogger.WriteLog("USER", "Info", "Program started at Main");

            Boolean PopMessageBox = Convert.ToBoolean(ConfigurationManager.AppSettings["Debug_MessageBox"]);
            Boolean WriteLogFile = Convert.ToBoolean(ConfigurationManager.AppSettings["Debug_LogFile"]);
            string tfsUri = ConfigurationManager.AppSettings["TFS_Uri"];
            string tfsLogin = ConfigurationManager.AppSettings["TFS_Login"];
            string tfsPassword = ConfigurationManager.AppSettings["TFS_Password"];
            string tfsDomain = ConfigurationManager.AppSettings["TFS_Domain"];
            string tfsTeamProject = ConfigurationManager.AppSettings["TFS_TeamProject"];
            string tfsWorkitemType = ConfigurationManager.AppSettings["TFS_WorkitemType"];
            string eToolAccountName = ConfigurationManager.AppSettings["Etool_AccountName"];
            string eToolUserName = ConfigurationManager.AppSettings["Etool_UserName"];
            string eToolUserPasswd = ConfigurationManager.AppSettings["Etool_Password"];
            string quickReportName = ConfigurationManager.AppSettings["Etool_QuickReport"];

            ElementoolClient eToolClient = new ElementoolClient(tfsUri, tfsLogin, tfsPassword, tfsDomain, tfsTeamProject, tfsWorkitemType, eToolAccountName, eToolUserName, eToolUserPasswd);
            eToolClient.PopMessageBox = PopMessageBox;
            eToolClient.WriteLogFile = WriteLogFile;

            List<int> issueNumbers = eToolClient.GetReportIssues(quickReportName);
            if (issueNumbers.Count > 0)
            {
                string fieldString = "";
                foreach (int issueNumber in issueNumbers)
                {
                    BugTrackingIssue issue = eToolClient.GetIssue(issueNumber);
                    if (issue != null)
                    {
                        fieldString = "External Work Item ID=" + issueNumber.ToString();
                        fieldString = fieldString + ";Title=" + eToolClient.GetIssueFieldBySystemName(issue, "title").Value;
                        fieldString = fieldString + ";Assigned To=" + eToolClient.GetIssueFieldBySystemName(issue, "assigned_to").Value;
                        fieldString = fieldString + ";State=" + "active";
                        eToolClient.UpdateTFS(issueNumber, fieldString);
                    }
                    else
                        if (WriteLogFile)
                            StaticLogger.WriteLog("Design", "Warning", "Could not find the issue - " + issueNumber);
                }
            }
            else
                if (PopMessageBox)
                    StaticLogger.WriteLog("Design", "Warning", "Could not find any issues from the quick report - " + quickReportName);
            StaticLogger.WriteLog("USER", "Info", "Program ended from Main");
        }
    }
}
